/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controls;

import com.jme3.asset.AssetManager;
import com.jme3.math.Vector3f;
import com.jme3.renderer.RenderManager;
import com.jme3.renderer.ViewPort;
import com.jme3.scene.Spatial;
import com.jme3.scene.control.AbstractControl;

/**
 *
 * @author dani
 */
public class EnemyControl extends AbstractControl {

    private Vector3f destination = new Vector3f(0, 15, 0);
    private float speed = 20f;

    public EnemyControl(AssetManager assetManager) {
    }

    @Override
    protected void controlUpdate(float tpf) {
        Spatial spatial = getSpatial();
        if ((Integer) spatial.getUserData("hp") > 0) {
            //move the enemy towards the "tower"
            Vector3f current = spatial.getLocalTranslation();
            Vector3f direction = destination.subtract(current).normalize();
            spatial.move(direction.mult(tpf * speed));
        } else {
            //destroy the enemy
            spatial.getParent().detachChild(spatial);
            System.out.println(spatial+" destroyed.");
        }
    }

    @Override
    protected void controlRender(RenderManager rm, ViewPort vp) {
    }
}
